package com.caigou.comm.base.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import com.caigou.comm.R
import com.caigou.comm.base.ext.hideKeyboard

/**
 * @author #Suyghur.
 * Created on 2022/02/24
 */
class ReportDialog(context: Context) : Dialog(context) {

    private lateinit var rgSelector: RadioGroup
    private lateinit var btn1: RadioButton
    private lateinit var btn2: RadioButton
    private lateinit var btn3: RadioButton
    private lateinit var btn4: RadioButton
    private lateinit var etOther: EditText
    private lateinit var btnCancel: Button
    lateinit var btnConfirm: Button

    var reportCause: String = "网投，卖料等广告信息"

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_report)

        rgSelector = findViewById(R.id.report_sg_selector)
        btn1 = findViewById(R.id.report_btn_1)
        btn2 = findViewById(R.id.report_btn_2)
        btn3 = findViewById(R.id.report_btn_3)
        btn4 = findViewById(R.id.report_btn_4)
        etOther = findViewById(R.id.report_et_other)
        btnCancel = findViewById(R.id.report_btn_cancel)
        btnConfirm = findViewById(R.id.report_btn_confirm)

        rgSelector.setOnCheckedChangeListener { _, checkedId ->
            reportCause = when (checkedId) {
                R.id.report_btn_1 -> {
                    etOther.clearFocus()
                    btn1.text.toString()
                }
                R.id.report_btn_2 -> {
                    etOther.clearFocus()
                    btn2.text.toString()
                }
                R.id.report_btn_3 -> {
                    etOther.clearFocus()
                    btn3.text.toString()
                }
                R.id.report_btn_4 -> {
                    etOther.clearFocus()
                    btn4.text.toString()
                }
                else -> ""
            }
        }

        etOther.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                reportCause = ""
                rgSelector.clearCheck()
            } else {
                v.hideKeyboard()
            }
        }

        btnCancel.setOnClickListener {
            dismiss()
        }
    }
}